package com.itheima.reggie.service.Impl;


import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.domain.DishFlavor;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishFlavorMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    @Override
    public Page<Dish> getdishPage(Page<Dish> page, String name) {
        LambdaQueryWrapper<Dish> wrapper=new LambdaQueryWrapper<>();
        if (!StringUtils.isEmpty(name)){
            wrapper.like(Dish::getName,name);
        }
        wrapper.orderByAsc(Dish::getId);
        page=dishMapper.selectPage(page,wrapper);
        List<Dish> dishList = page.getRecords();
        for (Dish dish : dishList) {
            Category category = categoryMapper.selectById(dish.getCategoryId());
            dish.setCategoryName(category.getName());
            LambdaQueryWrapper<DishFlavor> wrapper1=new LambdaQueryWrapper<>();
            wrapper1.eq(DishFlavor::getDishId,dish.getId());
            List<DishFlavor> dishFlavors = dishFlavorMapper.selectList(wrapper1);
            dish.setFlavors(dishFlavors);
        }
        return page;
    }

    @Override
    @Transactional
    public void saveAndFlavor(Dish dish) {
        dishMapper.insert(dish);
        List<DishFlavor> dishFlavorslist = dish.getFlavors();
        if (CollectionUtil.isNotEmpty(dishFlavorslist)) {
            for (DishFlavor dishFlavor : dishFlavorslist) {
                dishFlavor.setDishId(dish.getId());
                dishFlavorMapper.insert(dishFlavor);
            }
        }

    }

    @Override
    public Dish findById(long id) {
        Dish dish = dishMapper.selectById(id);
        LambdaQueryWrapper<DishFlavor> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(DishFlavor::getDishId,id);
        List<DishFlavor> dishFlavors = dishFlavorMapper.selectList(wrapper);
        dish.setFlavors(dishFlavors);
        return dish;
    }

    @Override
    @Transactional
    public void updateDash(Dish dish) {
        dishMapper.updateById(dish);
        LambdaQueryWrapper<DishFlavor> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(DishFlavor::getDishId,dish.getId());
        dishFlavorMapper.delete(wrapper);
        List<DishFlavor> dishFlavorslist = dish.getFlavors();
        if (CollectionUtil.isNotEmpty(dishFlavorslist)) {
            for (DishFlavor dishFlavor : dishFlavorslist) {
                dishFlavor.setDishId(dish.getId());
                dishFlavorMapper.insert(dishFlavor);
            }
        }
    }

    @Override
    @Transactional
    public void deleteByIds(Long[] ids) {
        LambdaQueryWrapper<Dish> wrapper1=new LambdaQueryWrapper<>();
        wrapper1.in(Dish::getId,ids);
        wrapper1.eq(Dish::getStatus,1);
        Integer count = dishMapper.selectCount(wrapper1);
        if (count>0){
            throw new CustomException("要删除的菜品还在在售状态，不能删除");
        }

        for (Long id : ids) {
            LambdaQueryWrapper<DishFlavor> wrapper=new LambdaQueryWrapper<>();
            wrapper.eq(DishFlavor::getDishId,id);
            dishFlavorMapper.delete(wrapper);
            dishMapper.deleteById(id);
        }
    }

    @Override
    @Transactional
    public void updateStatusByid(Integer status, Long[] ids) {
        for (Long id : ids) {
            Dish dish = new Dish();
            dish.setId(id);
            dish.setStatus(status);
            dishMapper.updateById(dish);
        }
    }

    @Override
    public List<Dish> selectByCategoryId(Long categoryId,Integer status) {
        LambdaQueryWrapper<Dish> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(Dish::getCategoryId,categoryId);
        wrapper.eq(Dish::getStatus,status);
        List<Dish> dishList = dishMapper.selectList(wrapper);

        if (CollectionUtil.isNotEmpty(dishList)) {
            for (Dish dish : dishList) {
                LambdaQueryWrapper<DishFlavor> wrapper1 = new LambdaQueryWrapper<>();
                wrapper1.eq(DishFlavor::getDishId,dish.getId());
                List<DishFlavor> dishFlavors = dishFlavorMapper.selectList(wrapper1);
                dish.setFlavors(dishFlavors);
            }
        }

        return dishList;
    }


}
